package com.wtx.chapter07;

/**
 * @description: 基于链表实现的集合
 * @author: wtx
 * @createDate: 2020/5/26
 */
public class LinkedSet<E> implements Set<E> {

    private LinkedList<E> list;

    public LinkedSet() {
        this.list = new LinkedList<>();
    }

    //不能添加重复元素
    @Override
    public void add(E e) {

        if (!list.contains(e))
            list.addFirst(e);  //在链表头添加元素时间复杂度较低
    }

    @Override
    public void remove(E e) {
        list.removeElement(e);
    }

    @Override
    public boolean contains(E e) {
        return list.contains(e);
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }
}
